/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.ui.internal.editor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.jfunction.internal.parser.JFunctionPartitionScanner;
import org.jfunction.ui.internal.JFunctionUIPlugin;

public class JFunctionDocumentProvider extends FileDocumentProvider {
	
	private IDocument document;

	public static final String JFUNCTION_PARTITIONING = "org.jfunction.partitioning"; //$NON-NLS-1$

	@Override
	protected IAnnotationModel createAnnotationModel(Object element)
			throws CoreException {
		// TODO Auto-generated method stub
		IAnnotationModel model = super.createAnnotationModel(element);
		model.connect(document);
		return model;
	}

	@Override
	protected void setupDocument(Object element,IDocument document) {
		this.document = document;
		if (document instanceof IDocumentExtension3) {
			IDocumentExtension3 ext= (IDocumentExtension3) document;
			JFunctionPartitionScanner scanner = new JFunctionPartitionScanner(JFunctionUIPlugin.getKeywords());
			IDocumentPartitioner partitioner = new FastPartitioner( scanner, new String[] {
				JFunctionPartitionScanner.SCENARIO,
				JFunctionPartitionScanner.EXAMPLES
			});
//			this.document = new Document();
			partitioner.connect(document);
			document.setDocumentPartitioner(partitioner);
			ext.setDocumentPartitioner(JFUNCTION_PARTITIONING, partitioner);
		}
	}

}
